import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:shop/common/common.dart';
import 'package:shop/common/widget/sms_counter_view.dart';
import 'package:shop/common/widget/textfield_view.dart';
import 'package:shop/login/widget/buttom_button.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final FocusNode _focusNode = FocusNode();
  final TextEditingController _controller = TextEditingController();
  final FocusNode _codefocusNode = FocusNode();
  final TextEditingController _codeController = TextEditingController();
  String phone = "";

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    _codeController.dispose();
    super.dispose();
  }

  void _regist() async {
    if (_controller.text.length < 11) {
      EasyLoading.showError("请输入11位手机号码");
      return;
    }
    if (_codeController.text.isEmpty) {
      EasyLoading.showError("请输入验证码");
      return;
    }
    EasyLoading.show(status: "Loading...");
    await Future.delayed(const Duration(seconds: 1));
    EasyLoading.dismiss();
    _jumpToRoot(context);
  }

  void _jumpPwdlogin() {
    // Navigator.pushNamed(context, "/pwdlogin", arguments: {"phone": _controller.text});
    Navigator.pop(context);
  }

  void _jumpToRoot(BuildContext context) {
    Navigator.of(context).pushNamedAndRemoveUntil('/root', (Route<dynamic> route) => false);
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return Scaffold(
      // resizeToAvoidBottomPadding: false,
      backgroundColor: Colors.white,
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          _focusNode.unfocus();
          _codefocusNode.unfocus();
        },
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(25),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  height: size.height * 0.05,
                ),
                Center(
                  child: Text(
                    "商城中心",
                    style: AppFont.textStyle(25, color: AppColor.font333, fontWeight: FontWeight.bold),
                  ),
                ),
                SizedBox(height: size.height * 0.1),
                NormalTextfield(
                  hintText: "请输入手机号码",
                  backgroundColor: Colors.white,
                  inputFormatters: [],
                  keyboardType: TextInputType.number,
                  focusNode: _focusNode,
                  maxLength: 11,
                  controller: _controller,
                  onSubmited: (val) {
                    setState(() {
                      phone = val;
                    });
                  },
                  onChanged: (val) {
                    print(val);
                    setState(() {
                      phone = val;
                    });
                  },
                ),
                SizedBox(height: 1, child: Divider()),
                SizedBox(
                  height: 8,
                ),
                Row(
                  children: [
                    Expanded(
                      child: NormalTextfield(
                        hintText: "请输入验证码",
                        backgroundColor: Colors.white,
                        maxLength: 6,
                        inputFormatters: [],
                        keyboardType: TextInputType.number,
                        focusNode: _codefocusNode,
                        controller: _codeController,
                      ),
                    ),
                    SmsCounterView(
                      phone: phone,
                    )
                  ],
                ),
                SizedBox(height: 1, child: Divider()),
                BottomButton(
                  margin: const EdgeInsets.only(top: 75, left: 0, right: 0),
                  title: "登录",
                  onTap: _regist,
                ),
                Spacer(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: () => Navigator.pushNamed(context, "/forgetpwd"),
                      child: Text(
                        "忘记密码?",
                        style: AppFont.textStyle(14, color: AppColor.fontgrey),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
